package cn.itcast.yimiaoyuyue.Adapter;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import cn.itcast.yimiaoyuyue.Adapter.R;
import cn.itcast.yimiaoyuyue.Bean.RegistMyBean;

public class RegistActivity extends AppCompatActivity {
    ImageView imBack;
    EditText etName;
    RadioGroup btnRg;
    RadioButton btnMan;
    RadioButton btnWomen;
    TextView Tv;
    EditText etBirthday;
    EditText etTelephone;
    EditText etPassword;
    Button btRegist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        RadioGroup rg = (RadioGroup) findViewById(R.id.btn_rg);
        final TextView tv = (TextView) findViewById(R.id.tv);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId){
                String tip = checkedId == R.id.btn_man ? "男" : "女";
                tv.setVisibility(View.VISIBLE);
                tv.setText(tip);
                if (checkedId==R.id.btn_man){
                    Toast.makeText(getApplicationContext(),"你选择了男",
                            Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(),"你选择了女",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        initView();
        initData();
        initEvent();

    }

    private void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });

        btRegist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }

         });

    }
    private void getData() {
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url = "http://10.1.63.53:8080/pro2/user/register";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name",etName.getText().toString());
            jsonObject.put("password", etPassword.getText().toString());
            jsonObject.put("sex",Tv.getText().toString());
            jsonObject.put("date",etBirthday.getText().toString());
            jsonObject.put("telephone", etTelephone.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: " + jsonObject);
                /*处理结果数据*/
                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                Gson gson = new Gson();
                RegistMyBean myBean = gson.fromJson(jsonObject.toString(), RegistMyBean.class);
                if(myBean.isSuccess()){
                    Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                            startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(), "失败", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }



    private void initData() {

    }

    private void initView() {
        imBack = findViewById(R.id.im_back);
        etName = findViewById(R.id.et_name);
        btnRg = findViewById(R.id.btn_rg);
        btnMan = findViewById(R.id.btn_man);
        btnWomen = findViewById(R.id.btn_woman);
        Tv = findViewById(R.id.tv);
        etBirthday = findViewById(R.id.et_birthday);
        etTelephone = findViewById(R.id.et_telephone);
        etPassword = findViewById(R.id.et_password);
        btRegist = findViewById(R.id.bt_regist);
    }
}